package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.CodeGenerator;
import com.zzyl.nursing.domain.dto.CheckInApplyDto;
import com.zzyl.nursing.domain.dto.CheckInConfigDto;
import com.zzyl.nursing.domain.dto.CheckInContractDto;
import com.zzyl.nursing.domain.dto.CheckInElderDto;
import com.zzyl.nursing.domain.pojo.*;
import com.zzyl.nursing.domain.vo.CheckInConfigVo;
import com.zzyl.nursing.domain.vo.CheckInDetailVo;
import com.zzyl.nursing.domain.vo.CheckInElderVo;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.service.ICheckInService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 入住Service业务层处理
 * 
 * @author alexis
 * @date 2025-05-19
 */
@Service
@RequiredArgsConstructor
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService
{
    @Autowired
    private CheckInMapper checkInMapper;

    private final ElderMapper elderMapper;



    /**
     * 查询入住
     * 
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id)
    {
        return checkInMapper.selectById(id);
    }

    /**
     * 查询入住列表
     * 
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn)
    {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn)
    {
        return checkInMapper.insert(checkIn);
    }

    /**
     * 修改入住
     * 
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn)
    {
        return checkInMapper.updateById(checkIn);
    }

    /**
     * 批量删除入住
     * 
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids)
    {
        return checkInMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除入住信息
     * 
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id)
    {
        return checkInMapper.deleteById(id);
    }



    private final BedMapper bedMapper;
    private final ContractMapper contractMapper;
    private final CheckInConfigMapper checkInConfigMapper;

    @Override
    public void apply(CheckInApplyDto checkInApplyDto) {
        CheckInElderDto checkInElderDto = checkInApplyDto.getCheckInElderDto();
        Elder elder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery().eq(Elder::getName, checkInElderDto.getName()).eq(Elder::getIdCardNo
                , checkInElderDto.getIdCardNo()));

        if(ObjectUtils.isNotEmpty(elder) && elder.getStatus() == 1){

            throw new ServiceException("老人已入住");
        }

        CheckInConfigDto checkInConfigDto = checkInApplyDto.getCheckInConfigDto();

        Bed bed = bedMapper.selectBedById(checkInConfigDto.getBedId());
        bed.setBedStatus(1);
        bedMapper.updateById(bed);

        Elder elder1 = new Elder();
        BeanUtil.copyProperties(checkInElderDto,elder1);
        elder1.setBedId(bed.getId());
        elder1.setBedNumber(bed.getBedNumber());
        if(ObjectUtils.isNotEmpty(elder)){
            elder1.setId((elder.getId()));
            elderMapper.updateElder(elder1);
        }else {
            elderMapper.insert(elder1);
        }

        CheckInContractDto checkInContractDto = checkInApplyDto.getCheckInContractDto();
        Contract contract = BeanUtil.toBean(checkInContractDto, Contract.class);
        contract.setElderId(elder1.getId());
        contract.setElderName(elder1.getName());
        contract.setStartDate(checkInConfigDto.getStartDate());
        contract.setEndDate(checkInConfigDto.getEndDate());
        contract.setStatus(checkInConfigDto.getStartDate().isBefore(LocalDateTime.now())?1:0);
        contract.setContractNumber("HT"+CodeGenerator.generateContractNumber());
        contractMapper.insert(contract);

        CheckIn checkIn = BeanUtil.toBean(checkInConfigDto, CheckIn.class);
        checkIn.setBedNumber(bed.getBedNumber());
        checkIn.setIdCardNo(elder1.getIdCardNo());
        checkIn.setElderId(elder1.getId());
        checkIn.setElderName(elder1.getName());
        checkInMapper.insert(checkIn);

        CheckInConfig checkInConfig = BeanUtil.toBean(checkInConfigDto, CheckInConfig.class);
        checkInConfig.setCheckInId(checkIn.getId());
        checkInConfigMapper.insert(checkInConfig);


    }

    @Override
    public CheckInDetailVo detail(Long id) {
        //返回对象
        CheckInDetailVo checkInDetailVo = new CheckInDetailVo();
        //checkin对象
        CheckIn checkIn = this.selectCheckInById(id);
        //将checkin转为checkInElderVo对象
        CheckInConfigVo checkInConfigVo = BeanUtil.toBean(checkIn, CheckInConfigVo.class);
        //checkInConfig对象
        CheckInConfig checkInConfig = checkInConfigMapper.selectOne(Wrappers.<CheckInConfig>lambdaQuery().eq(CheckInConfig::getCheckInId, checkIn.getId()));
        Elder elder = elderMapper.selectElderById(checkIn.getElderId());
        CheckInElderVo checkInElderVo = BeanUtil.toBean(elder, CheckInElderVo.class);
        //将checkInConfig转为checkInConfigVo对象
         checkInConfigVo = BeanUtil.toBean(checkInConfig, CheckInConfigVo.class);
        Contract contract = contractMapper.selectOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getElderId, checkIn.getElderId()));
        checkInDetailVo.setCheckInConfigVo(checkInConfigVo);
        checkInDetailVo.setContract(contract);
        checkInDetailVo.setCheckInElderVo(checkInElderVo);

        return checkInDetailVo;
    }
}
